package io.renren.controller;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.renren.entity.*;
import io.renren.entity.VO.PersonDispatchEntityVO;
import io.renren.service.EmpSyncToB1Service;
import io.renren.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.service.PersonDispatchService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 人员派遣表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-29 01:37:51
 */
@RestController
@RequestMapping("persondispatch")
public class PersonDispatchController extends AbstractController {
	@Autowired
	private PersonDispatchService personDispatchService;

	@Autowired
	EmpSyncToB1Service empSyncToB1Service;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("persondispatch:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		try {
//			empSyncToB1Service.empSynToB1();
			empSyncToB1Service.addEmployeeSettleSyncToB1();

		} catch (Exception e) {
			e.printStackTrace();
		}

		List<PersonDispatchEntityVO> personDispatchList = personDispatchService.queryList(query);
		int total = personDispatchService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(personDispatchList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("persondispatch:info")
	public R info(@PathVariable("id") String id){
		PersonDispatchEntity personDispatch = personDispatchService.queryObject(id);
		
		return R.ok().put("personDispatch", personDispatch);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("persondispatch:save")
	public R save(@RequestBody PersonDispatchEntity personDispatch){
		personDispatchService.save(personDispatch);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("persondispatch:update")
	public R update(@RequestBody PersonDispatchEntity personDispatch){
		personDispatchService.update(personDispatch);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("persondispatch:delete")
	public R delete(@RequestBody String[] ids){
		personDispatchService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 审核
	 */
	@RequestMapping("/confirm")
	@RequiresPermissions("persondispatch:update")
	public R delete(@RequestBody JSONObject jsonObject){
        SysUserEntity user = getUser();
        jsonObject.put("user", user);
		int res = personDispatchService.confirm(jsonObject);
		if(res == 1){
			return R.ok().put("code","1").put("msg","员工所属结算点没有配置社保区域基数信息，请先配置社保");
		}else
			return R.ok();
	}

	@RequestMapping("/statement")
	@RequiresPermissions("persondispatch:list")//confirm
	public R statement(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("settlementid",params.get("settlementid"));
		//jsonObject.getString("year");
		List<ServiceSettlementDetailEntity> list = personDispatchService.statement(params);
		if(list == null){
			list = new ArrayList<>();
		}
		PageUtils pageUtil = new PageUtils(list, list.size(), query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	@RequestMapping("/saveDetail")
	@RequiresPermissions("persondispatch:save")
	public R saveStatementDetail(@RequestBody JSONArray jsonArray){
		personDispatchService.saveStatementDetail(jsonArray);
		return R.ok();
	}

	@RequestMapping("/salary")
	@RequiresPermissions("persondispatch:list")//confirm
	public R salary(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("settlementid",params.get("settlementid"));
		//jsonObject.getString("year");
		List<SalaryBasicinfoEntity> list = personDispatchService.salary(params);
		if(list == null){
			list = new ArrayList<>();
		}
		PageUtils pageUtil = new PageUtils(list, list.size(), query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	@RequestMapping("saveSalary")
	@RequiresPermissions("persondispatch:save")
	public R saveSalary(@RequestBody JSONArray jsonArray){
		personDispatchService.saveSalary(jsonArray);
		return R.ok();
	}

	@RequestMapping("/getListinfo")
	@RequiresPermissions("empbaseinfo:select")
	public R getListinfo(){
		Map<String, Object> map = personDispatchService.getListInfo(new String[]{"archType","operation","costFunc","costType","station"});
		return R.ok(map);
	}

	@RequestMapping("/getSysOffice")
	@RequiresPermissions("empbaseinfo:select")
	public R getSysOffice(){
		Map<String, Object> map = personDispatchService.getSysOffice();
		return R.ok(map);
	}
	
}
